﻿using IOA.MES.Business;
using IOA.MES.DataAccess.Entity;
using IOA.MES.DataAccess.Enum;
using IOA.MES.DataAccess.VModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;

namespace IOA.MES.Web.Controllers
{
    public class WarehouseController : Controller
    {
        #region 仓库管理
        /// <summary>
        /// 仓库管理
        /// </summary>
        public ActionResult WarehouseMgr()
        {
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            return View();
        }

        /// <summary>
        /// 仓库编辑
        /// </summary>
        public ActionResult WarehouseEdit(int warehouseId = 0)
        {
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            var warehouse = warehouseId <= 0 ? null : new WarehouseMgr().GetWarehouse(warehouseId);
            return View(warehouse);
        }

        /// <summary>
        /// 仓库编辑接口
        /// </summary>
        [HttpPost]
        public JsonResult EditWarehouse(VWarehouse model)
        {
            if (model.PkId <= 0)
            {
                model.CreateBy = BaseEnv.UserId;
            }
            else
            {
                model.ModifyBy = BaseEnv.UserId;
            }
            new WarehouseMgr().EditWarehouse(model);
            return Json(new ResultJson<object>());
        }

        /// <summary>
        /// 修改仓库状态
        /// </summary>
        [HttpPost]
        public JsonResult ModifyWarehouseStatus(int warehouseId, CommonStatus status)
        {
            new WarehouseMgr().ModifyStatus(new VWarehouse { PkId = warehouseId, Status = status, ModifyBy = BaseEnv.UserId });
            return Json(new ResultJson<object>());
        }

        /// <summary>
        /// 查询仓库列表
        /// </summary>
        [HttpPost]
        public JsonResult QueryWarehouses(WarehouseType? warehouseType, string workshopCode, CommonStatus? status)
        {
            var warehouses = new WarehouseMgr().QueryWarehouses(warehouseType, workshopCode, status);
            return Json(new ResultJson<object>(warehouses, warehouses.Count));
        }

        /// <summary>
        /// 获取所有仓库
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetAllWarehouse()
        {
            var warehouses = new WarehouseMgr().QueryWarehouses();
            return Json(new ResultJson<object>(warehouses));
        }
        #endregion

        #region 仓库用户管理
        /// <summary>
        /// 仓库用户管理
        /// </summary>
        public ActionResult WarehouseUserEdit(string warehouseCode)
        {
            var warehouse = new WarehouseMgr().GetWarehouse(warehouseCode);
            return View(warehouse);
        }

        /// <summary>
        /// 添加仓库用户
        /// </summary>
        [HttpPost]
        public JsonResult AddWarehouseUser(string warehouseCode, string userName)
        {
            var user = new WarehouseMgr().AddWarehouseUser(warehouseCode, userName, BaseEnv.UserId);
            return Json(new ResultJson<object>(user));
        }

        /// <summary>
        /// 移除仓库用户
        /// </summary>
        [HttpPost]
        public JsonResult RemoveWarehouseUser(string warehouseCode, string userName)
        {
            new WarehouseMgr().RemoveWarehouseUser(warehouseCode, userName, BaseEnv.UserId);
            return Json(new ResultJson<object>());
        }

        /// <summary>
        /// 移除仓库用户
        /// </summary>
        [HttpPost]
        public JsonResult QueryWarehouseUsers(string warehouseCode)
        {
            var users = new WarehouseMgr().QueryWarehouseUsers(warehouseCode);
            return Json(new ResultJson<object>(users));
        }
        #endregion

        #region 库区管理
        /// <summary>
        /// 库区管理
        /// </summary>
        public ActionResult WarehouseAreaMgr()
        {
            ViewBag.Warehouses = new WarehouseMgr().QueryWarehouses();
            return View();
        }

        /// <summary>
        /// 库区编辑
        /// </summary>
        public ActionResult WarehouseAreaEdit(int areaId = 0)
        {
            ViewBag.Warehouses = new WarehouseMgr().QueryWarehouses();
            var area = areaId <= 0 ? null : new WarehouseMgr().GetWarehouseArea(areaId);
            return View(area);
        }

        /// <summary>
        /// 库区编辑接口
        /// </summary>
        [HttpPost]
        public JsonResult EditWarehouseArea(VWarehouseArea model)
        {
            if (model.PkId <= 0)
            {
                model.CreateBy = BaseEnv.UserId;
            }
            else
            {
                model.ModifyBy = BaseEnv.UserId;
            }
            new WarehouseMgr().EditWarehouseArea(model);
            return Json(new ResultJson<object>());
        }

        /// <summary>
        /// 修改库区状态
        /// </summary>
        [HttpPost]
        public JsonResult ModifyWarehouseAreaStatus(int areaId, CommonStatus status)
        {
            new WarehouseMgr().ModifyAreaStatus(new VWarehouseArea { PkId = areaId, Status = status, ModifyBy = BaseEnv.UserId });
            return Json(new ResultJson<object>());
        }

        /// <summary>
        /// 查询库区列表
        /// </summary>
        [HttpPost]
        public JsonResult QueryWarehouseAreas(int page, int limit, string warehouseCode, string key, CommonStatus? status)
        {
            var total = 0;
            var areas = new WarehouseMgr().QueryWarehouseAreas(page, limit, out total, warehouseCode, key, status);
            return Json(new ResultJson<object>(areas, areas.Count));
        }
        #endregion

        #region 库位管理
        /// <summary>
        /// 库位管理
        /// </summary>
        public ActionResult WarehouseLocationMgr()
        {
            ViewBag.Warehouses = new WarehouseMgr().QueryWarehouses();
            return View();
        }

        /// <summary>
        /// 库位编辑
        /// </summary>
        public ActionResult WarehouseLocationEdit(int locationId = 0)
        {
            ViewBag.Warehouses = new WarehouseMgr().QueryWarehouses();
            var location = locationId <= 0 ? null : new WarehouseMgr().GetWarehouseLocation(locationId);
            return View(location);
        }

        /// <summary>
        /// 库位编辑接口
        /// </summary>
        [HttpPost]
        public JsonResult EditWarehouseLocation(VWarehouseLocation model)
        {
            if (model.PkId <= 0)
            {
                model.CreateBy = BaseEnv.UserId;
            }
            else
            {
                model.ModifyBy = BaseEnv.UserId;
            }
            new WarehouseMgr().EditWarehouseLocation(model);
            return Json(new ResultJson<object>());
        }

        /// <summary>
        /// 修改库位状态
        /// </summary>
        [HttpPost]
        public JsonResult ModifyWarehouseLocationStatus(int locationId, CommonStatus status)
        {
            new WarehouseMgr().ModifyLocationStatus(new VWarehouseLocation { PkId = locationId, Status = status, ModifyBy = BaseEnv.UserId });
            return Json(new ResultJson<object>());
        }

        /// <summary>
        /// 查询库位列表
        /// </summary>
        [HttpPost]
        public JsonResult QueryWarehouseLocations(int page, int limit, string warehouseCode, string areaCode, string key, CommonStatus? status)
        {
            var total = 0;
            var locations = new WarehouseMgr().QueryWarehouseLocations(page, limit, out total, warehouseCode, areaCode, key, status);
            return Json(new ResultJson<object>(locations, locations.Count));
        }
        #endregion

        #region 入库管理
        /// <summary>
        /// 待入库管理
        /// </summary>
        public ActionResult UnInboundMgr()
        {
            var total = 0;
            ViewBag.Workshops = new WorkshopMgr().QueryWorkshops(1, 0, out total, null, "");
            return View();
        }

        /// <summary>
        /// 查询待入库组件
        /// </summary>
        /// <param name="page">页面索引</param>
        /// <param name="limit">页面容量</param>
        /// <param name="workshopCode">车间ID</param>
        /// <param name="orderNo">订单号</param>
        /// <param name="workOrderNo">工单号</param>
        /// <param name="packBatchNo">包装批次号</param>
        /// <param name="packNo">包装号</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">截止日期</param>
        [HttpPost]
        public JsonResult QueryUnInboundPacks(int page, int limit, string workshopCode, string orderNo, string workOrderNo, string packBatchNo, string packNo, DateTime startDate, DateTime endDate)
        {
            var total = 0;
            var packs = new WarehouseMgr().QueryUnInboundPacks(page, limit, out total, workshopCode, orderNo, workOrderNo, packBatchNo, packNo, startDate, endDate);

            return Json(new { ErrCode = 200, Data = packs, TotalCount = total });
        }

        /// <summary>
        /// 入库单管理
        /// </summary>
        public ActionResult InboundTaskMgr()
        {
            ViewBag.Warehouses = new WarehouseMgr().QueryWarehouses(WarehouseType.Product, string.Empty, CommonStatus.Normal);
            return View();
        }

        /// <summary>
        /// 查询入库单列表
        /// </summary>
        /// <param name="page">页面索引</param>
        /// <param name="limit">页面容量</param>
        /// <param name="warehouseCode">仓库代码</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">截止日期</param>
        public JsonResult QueryInboundTasks(int page, int limit, string warehouseCode, DateTime startDate, DateTime endDate)
        {
            var total = 0;
            var tasks = new InboundMgr().QueryInbounds(page, limit, out total, warehouseCode, InboundType.ProductInbound, startDate, endDate);
            return Json(new { ErrCode = 200, Data = tasks, TotalCount = total });
        }

        /// <summary>
        /// 入库详情
        /// </summary>
        public ActionResult InboundDetail(string inboundNo)
        {
            var inbound = new InboundMgr().GetInbound(inboundNo);
            return View(inbound);
        }
        #endregion

        #region 发货出库
        /// <summary>
        /// 发货单管理
        /// </summary>
        public ActionResult DeliveryOrderMgr()
        {
            ViewBag.Warehouses = new WarehouseMgr().QueryWarehouses(WarehouseType.Product, string.Empty, CommonStatus.Normal);
            return View();
        }

        /// <summary>
        /// 查询发货单
        /// </summary>
        /// <param name="page">页面索引</param>
        /// <param name="limit">页面容量</param>
        /// <param name="warehouseCode">仓库代码</param>
        /// <param name="deliveryOrderNo">发货单号</param>
        /// <param name="orderNo">合同编号</param>
        /// <param name="customer">客户信息</param>
        /// <param name="product">组件信息</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">截止日期</param>
        [HttpPost]
        public JsonResult QueryDeliveryOrders(int page, int limit, string warehouseCode, string deliveryOrderNo, string orderNo, string customer, DateTime startDate, DateTime endDate)
        {
            var total = 0;
            var orders = new DeliveryMgr().QueryDeliveryOrders(page, limit, out total, warehouseCode, deliveryOrderNo, orderNo, customer, startDate, endDate);

            return Json(new { ErrCode = 200, Data = orders, TotalCount = total });
        }

        /// <summary>
        /// 出库单管理
        /// </summary>
        public ActionResult OutboundTaskMgr()
        {
            ViewBag.Warehouses = new WarehouseMgr().QueryWarehouses(WarehouseType.Product, string.Empty, CommonStatus.Normal);
            return View();
        }

        /// <summary>
        /// 查询入库单列表
        /// </summary>
        /// <param name="page">页面索引</param>
        /// <param name="limit">页面容量</param>
        /// <param name="warehouseCode">仓库代码</param>
        /// <param name="deliveryNo">发货单号</param>
        /// <param name="outboundNo">出库单号</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">截止日期</param>
        public JsonResult QueryOutboundTasks(int page, int limit, string warehouseCode, string deliveryNo, string outboundNo, DateTime startDate, DateTime endDate)
        {
            var total = 0;
            var tasks = new OutboundMgr().QueryOutbounds(page, limit, out total, warehouseCode, deliveryNo, outboundNo, startDate, endDate);
            return Json(new { ErrCode = 200, Data = tasks, TotalCount = total });
        }

        /// <summary>
        /// 出库单详情
        /// </summary>
        public ActionResult OutboundDetail(string outboundNo)
        {
            var outbound = new OutboundMgr().GetOutbound(outboundNo);
            return View(outbound);
        }
        #endregion

        #region 库位库存
        /// <summary>
        /// 库位库存页面
        /// </summary>
        public ActionResult LocationStorage()
        {
            ViewBag.Warehouses = new WarehouseMgr().QueryUserWarehouses(BaseEnv.UserId).Where(_ => _.WarehouseType == WarehouseType.Product).ToList();
            return View();
        }

        /// <summary>
        /// 查询库位库存
        /// </summary>
        [HttpPost]
        public JsonResult QueryLocationStorages(int page, int limit, string warehouseCode, string areaCode, string location)
        {
            var total = 0;
            var storages = new WarehouseMgr().QueryLocationStorages(page, limit, out total, warehouseCode, areaCode, location);
            return Json(new ResultJson<object>(storages, total));
        }
        #endregion

        #region 货品库存
        /// <summary>
        /// 货品库存页面
        /// </summary>
        public ActionResult ProductStorage()
        {
            ViewBag.Customers = new CustomerMgr().QueryCustomers();
            return View();
        }

        /// <summary>
        /// 查询货品库存
        /// </summary>
        [HttpPost]
        public JsonResult QueryProductStorages(int page, int limit, string customerCode, string orderNo, string workOrderNo, string cabinetNo, string packNo)
        {
            var total = 0;
            var storages = new WarehouseMgr().QueryProductStorages(page, limit, out total, customerCode, orderNo, workOrderNo, cabinetNo, packNo);
            return Json(new ResultJson<object>(storages, total));
        }

        /// <summary>
        /// 导出货品库存
        /// </summary>
        public void ExportProductStorages(string customerCode, string orderNo, string workOrderNo, string cabinetNo, string packNo)
        {
            var total = 0;
            var storages = new WarehouseMgr().QueryProductStorages(1, 0, out total, customerCode, orderNo, workOrderNo, cabinetNo, packNo);

            var book = new XSSFWorkbook();
            var sheet = book.CreateSheet("货品库存");
            for (int i = 0; i <= 22; i++)
            {
                sheet.SetColumnWidth(i, 15 * 256 + 200);
            }

            var styleCenter = book.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.VerticalAlignment = VerticalAlignment.Center;
            styleCenter.BorderBottom = BorderStyle.Thin;
            styleCenter.BorderLeft = BorderStyle.Thin;
            styleCenter.BorderRight = BorderStyle.Thin;
            styleCenter.BorderTop = BorderStyle.Thin;

            var row1 = sheet.CreateRow(0);
            row1.Height = 30 * 20;
            row1.CreateCell(0).SetCellValue("序号");
            row1.CreateCell(1).SetCellValue("仓库");
            row1.CreateCell(2).SetCellValue("库位");
            row1.CreateCell(3).SetCellValue("客户仓库");
            row1.CreateCell(4).SetCellValue("客户名称");
            row1.CreateCell(5).SetCellValue("订单号");
            row1.CreateCell(6).SetCellValue("工单号");
            row1.CreateCell(7).SetCellValue("生产车间");
            row1.CreateCell(8).SetCellValue("柜号");
            row1.CreateCell(9).SetCellValue("打包编号");
            row1.CreateCell(10).SetCellValue("成品料号");
            row1.CreateCell(11).SetCellValue("成品名称");
            row1.CreateCell(12).SetCellValue("成品规格");
            row1.CreateCell(13).SetCellValue("包装数量");
            row1.CreateCell(14).SetCellValue("组件数量");
            row1.CreateCell(15).SetCellValue("等级");
            row1.CreateCell(16).SetCellValue("功率");
            row1.CreateCell(17).SetCellValue("铭牌型号");
            row1.CreateCell(18).SetCellValue("档位");
            row1.CreateCell(19).SetCellValue("组件颜色");
            row1.CreateCell(20).SetCellValue("电池片规格");
            row1.CreateCell(21).SetCellValue("电池片厂家");
            row1.CreateCell(22).SetCellValue("入库时间");
            for (int i = 0; i <= 22; i++)
            {
                row1.GetCell(i).CellStyle = styleCenter;
            }

            if (!storages.Any())
            {
                var ms1 = new MemoryStream();
                book.Write(ms1);
                Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"{DateTime.Now:yyyyMMddHHmmss}货品库存"));
                Response.BinaryWrite(ms1.ToArray());
                book = null;
                ms1.Close();
                ms1.Dispose();
                return;
            }
            for (int i = 0; i < storages.Count; i++)
            {
                var tmpRow = sheet.CreateRow(i + 1);
                tmpRow.Height = 30 * 20;
                tmpRow.CreateCell(0).SetCellValue(i + 1);
                tmpRow.CreateCell(1).SetCellValue(storages[i].WarehouseName);
                tmpRow.CreateCell(2).SetCellValue(storages[i].Location);
                tmpRow.CreateCell(3).SetCellValue(storages[i].CustomerWarehouseName);
                tmpRow.CreateCell(4).SetCellValue(storages[i].CustomerName);
                tmpRow.CreateCell(5).SetCellValue(storages[i].OrderNo);
                tmpRow.CreateCell(6).SetCellValue(storages[i].WorkOrderNo);
                tmpRow.CreateCell(7).SetCellValue(storages[i].WorkshopName);
                tmpRow.CreateCell(8).SetCellValue(storages[i].CabinetNo);
                tmpRow.CreateCell(9).SetCellValue(storages[i].PackNo);
                tmpRow.CreateCell(10).SetCellValue(storages[i].ProductCode);
                tmpRow.CreateCell(11).SetCellValue(storages[i].ProductName);
                tmpRow.CreateCell(12).SetCellValue(storages[i].ProductSpecs);
                tmpRow.CreateCell(13).SetCellValue((double)storages[i].Quantity);
                tmpRow.CreateCell(14).SetCellValue((double)storages[i].DetailQuantity);
                tmpRow.CreateCell(15).SetCellValue(storages[i].QLevel);
                tmpRow.CreateCell(16).SetCellValue((double)(storages[i].PowerLevel ?? 0));
                tmpRow.CreateCell(17).SetCellValue(storages[i].NameplateModel);
                tmpRow.CreateCell(18).SetCellValue(storages[i].CurrentLevel);
                tmpRow.CreateCell(19).SetCellValue(storages[i].ComponentColor);
                tmpRow.CreateCell(20).SetCellValue(storages[i].BatterySpecs);
                tmpRow.CreateCell(21).SetCellValue(storages[i].BatteryFactory);
                tmpRow.CreateCell(22).SetCellValue(storages[i].CreateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                for (int j = 0; j <= 22; j++)
                {
                    tmpRow.GetCell(j).CellStyle = styleCenter;
                }
            }

            var ms = new MemoryStream();
            book.Write(ms);
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"{DateTime.Now:yyyyMMddHHmmss}货品库存"));
            Response.BinaryWrite(ms.ToArray());
            book = null;
            ms.Close();
            ms.Dispose();
        }
        #endregion

        #region 拼柜管理
        /// <summary>
        /// 拼柜管理
        /// </summary>
        public ActionResult CabinetMgr()
        {
            ViewBag.Warehouses = new WarehouseMgr().QueryUserWarehouses(BaseEnv.UserId);
            return View();
        }

        /// <summary>
        /// 查询拼柜列表
        /// </summary>
        [HttpPost]
        public JsonResult QueryCabinets(int page, int limit, string warehouseCode, string orderNo, string workOrderNo, string cabinetNo, DateTime? startTime, DateTime? endTime, CabinetStatus? status)
        {
            var total = 0;
            var cabinets = new CabinetMgr().QueryCabinets(page, limit, out total, warehouseCode, orderNo, workOrderNo, cabinetNo, startTime, endTime, status);

            return Json(new ResultJson<object>(cabinets, total));
        }

        /// <summary>
        /// 拼柜页
        /// </summary>
        public ActionResult CabinetEdit(string cabinetNo = "")
        {
            var cabinet = string.IsNullOrWhiteSpace(cabinetNo) ? null : new CabinetMgr().GetCabinet(cabinetNo);
            return View(cabinet);
        }

        /// <summary>
        /// 创建新柜
        /// </summary>
        [HttpPost]
        public JsonResult CreateCabinet(string cabinetNo)
        {
            new CabinetMgr().CreateCabinet(new VCabinet { CabinetNo = cabinetNo, CreateBy = BaseEnv.UserId });
            return Json(new { ErrCode = 200 });
        }

        /// <summary>
        /// 包装拼柜
        /// </summary>
        [HttpPost]
        public JsonResult CabinetPack(string cabinetNo, string packNo)
        {
            VCabinetCondition cabinetCondition = null;
            new CabinetMgr().CabinetPack(new VCabinet { CabinetNo = cabinetNo, CreateBy = BaseEnv.UserId }, new List<string> { packNo }, out cabinetCondition);
            return Json(new { ErrCode = 200, Data = cabinetCondition });
        }

        /// <summary>
        /// 包装拼柜
        /// </summary>
        [HttpPost]
        public JsonResult FinishCabinet(string cabinetNo)
        {
            new CabinetMgr().FinishCabinet(new VCabinet { CabinetNo = cabinetNo, ModifyBy = BaseEnv.UserId });
            return Json(new { ErrCode = 200 });
        }

        /// <summary>
        /// 单个解柜
        /// </summary>
        [HttpPost]
        public JsonResult UnCabinetPack(string packNo)
        {
            new CabinetMgr().UnCabinetPack(new VCabinetPack { PackNo = packNo, ModifyBy = BaseEnv.UserId });
            return Json(new { ErrCode = 200 });
        }

        /// <summary>
        /// 拼柜详情
        /// </summary>
        public ActionResult CabinetDetail(string cabinetNo)
        {
            var cabinet = new CabinetMgr().GetCabinet(cabinetNo);
            return View(cabinet);
        }

        /// <summary>
        /// 解柜
        /// </summary>
        [HttpPost]
        public JsonResult UnCabinet(string cabinetNo)
        {
            new CabinetMgr().UnCabinet(new VCabinet
            {
                CabinetNo = cabinetNo,
                ModifyBy = BaseEnv.UserId
            });
            return Json(new ResultJson<object>());
        }
        #endregion

        #region 装车检
        /// <summary>
        /// 装车检管理
        /// </summary>
        public ActionResult LoadInspectMgr()
        {
            ViewBag.LoadStatusEnums = new CommonMgr().QueryEnumTypes(typeof(LoadStatus));
            return View();
        }

        /// <summary>
        /// 查询装车检列表
        /// </summary>
        [HttpPost]
        public JsonResult QueryLoadInspects(int page, int limit, string deliveryNo, DateTime startDate, DateTime endDate, LoadStatus? status)
        {
            var total = 0;
            var orders = new LoadMgr().QueryLoads(page, limit, out total, deliveryNo, startDate, endDate.AddDays(1), status);
            return Json(new ResultJson<object>(orders, total));
        }

        /// <summary>
        /// 出库单详情
        /// </summary>
        public ActionResult LoadInspectDetail(int loadId)
        {
            var outbound = new LoadMgr().GetLoad(loadId);
            return View(outbound);
        }
        #endregion

        #region 车间仓操作
        /// <summary>
        /// 车间仓库存
        /// </summary>
        public ActionResult WorkshopStorageMgr()
        {
            ViewBag.Warehouses = new WarehouseMgr().QueryUserWarehouses(BaseEnv.UserId);
            return View();
        }

        /// <summary>
        /// 查询车间仓库存
        /// </summary>
        [HttpPost]
        public JsonResult QueryWorkshopWarehouseStorages(int page, int limit, string warehouseCode, string category, string product, string batchNo, bool? storageStatus)
        {
            var warehouseCodes = !string.IsNullOrWhiteSpace(warehouseCode) ? new List<string> { warehouseCode } : new WarehouseMgr().QueryUserWarehouses(BaseEnv.UserId).Select(_ => _.WarehouseCode).ToList();
            int total = 0;
            var storages = new WarehouseMgr().QueryWorkshopWarehouseStorages(page, limit, out total, warehouseCodes, category, product, batchNo, storageStatus);
            return Json(new ResultJson<object>(storages, total));
        }

        /// <summary>
        /// 发料记录
        /// </summary>
        public ActionResult DispatchOrderMgr()
        {
            ViewBag.Warehouses = new WarehouseMgr().QueryUserWarehouses(BaseEnv.UserId);
            return View();
        }

        /// <summary>
        /// 查询发料记录
        /// </summary>
        [HttpPost]
        public JsonResult QueryDispatchOrders(int page, int limit, string warehouseCode, string category, string product, string batchNo, bool? storageStatus)
        {
            var warehouseCodes = !string.IsNullOrWhiteSpace(warehouseCode) ? new List<string> { warehouseCode } : new WarehouseMgr().QueryUserWarehouses(BaseEnv.UserId).Select(_ => _.WarehouseCode).ToList();
            int total = 0;
            var storages = new WarehouseMgr().QueryWorkshopWarehouseStorages(page, limit, out total, warehouseCodes, category, product, batchNo, storageStatus);
            return Json(new ResultJson<object>(storages, total));
        }
        #endregion
    }
}
